<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentBrowse.class.php, $Author:  $ */



/** Component for browsing groups
 *  Max Kovrigovich
 * @version $Id: PhpFox_ComponentBrowse.class.php
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentBrowse extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */

		$oSecurityService->checkPerm('groups', array('read_only', 'full'));

        //initialization

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUrl = &App::getUrl();
        /* @var $oUrl Url */

        $oGroupsSrv = $this->_oModule->getService('Groups');
        /* @var $oGroupsSrv PhpFox_Mod_Account_ServiceGroups */

        App::loadClass('Filter');
        App::loadClass('Pager');

        $iCategoryId = $oReq->getInt('type', 0);

        $aForm = array();
        $aForm['sortby'] = $oReq->get('sortby', 'time');
	    switch($aForm['sortby'])
        {
            case 'time': $sOrder = 'time DESC';break;
            case 'popular': $sOrder = 'views DESC';break;
            case 'name': $sOrder = 'title ASC';break;
            default: $sOrder = 'time DESC';$aForm['sortby'] = 'time'; break;
        }

        $aTmp = $oGroupsSrv->getCategories();

        $aCategories = array();
        $aCategories[''] = App::format('core.any');
        foreach($aTmp as $aItem)
        {
        	$aCategories[$aItem['id']] = '&nbsp;'.$aItem['name'];
        }

		$aLocation[''] = App::format('core.any');
        foreach (App::getTxtFile('country.txt') as $sVal)
        {
            $aLocation[$sVal] = $sVal;
        }

        $aSortBy = array
        (
            'popular' 	=> App::format('Mod_Groups.sortby.popular'),
            'name' 		=> App::format('Mod_Groups.sortby.name'),
            'time'  	=> App::format('Mod_Groups.sortby.time'),
        );

        $aFilter = array
        (
            'type' => array
            (
                'value'  => '=',
                'select' => $aCategories,
            	'def'	 => ($iCategoryId > 0)?$iCategoryId:'',
            ),
            'location' => array
            (
                'value'  => '=',
                'select' => $aLocation,
            ),
            'city' => 'like',
            'keywords' => 'like',
        );
        
        if (App::getSetting('us_states'))
        {  
        	$aStates = array();
        	$aStates[''] = App::format('core.any');
            foreach (App::getTxtFile('us_stat.txt') as $sState)
            {
                $aStates[$sState] = $sState;
            }     
        	$aFilter['state'] = array('value' => '=', 'select' => $aStates);
        	$aFilter['zip'] = 'like';
        }

        if($iCategoryId || ($oReq->getAction() != 'browse_group_show'))
        {
        	unset($_SESSION['filter']);
        }
        $oFilter = &new Filter('browse_group', $aFilter);

        $oFilter->activateFilter();

        unset($oFilter->aConditions['keywords']);
	$searchKeywords=$oReq->getArray('browse_group');
	if (isset($searchKeywords['keywords']))
	{
	    $searchKeywords=DataBase::escapeLike($searchKeywords['keywords']);
	}
	$sAddCondition='';
	if(!empty($searchKeywords))
		$sAddCondition='AND ( gm.title LIKE "%'.$searchKeywords.'%" OR gm.text LIKE "%'.$searchKeywords.'%")';

		$aCond = array();
		foreach ($oFilter->aConditions as $sCond)
		{
			$aCond[] = $sCond;
		}

        list($aItems, $iCnt) = $oGroupsSrv->searchGroups($aCond, $oReq->getInt('page', 1), App::getParam('public.pagesize'), $sOrder, $sAddCondition);

        $iPageSize = App::getParam('public.pagesize');
        $iPage = $oReq->getInt('page', 1);
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);

        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $oTpl = $this->_oModule->getTemplate();

        $sCategory = App::format('Mod_Groups.allgroups');
        $iCatId = $oFilter->aData['type'];

        if($iCatId != 0)
	        $sCategory = str_replace('&nbsp;', '', $aCategories[$iCatId]);

		if($oFilter->aData['type'])$oUrl->setParam('type', $oFilter->aData['type']);
		if($oFilter->aData['location']) $oUrl->setParam('location', $oFilter->aData['location']);
		if($oFilter->aData['city']) $oUrl->setParam('city', $oFilter->aData['city']);
		if(isset($oFilter->aData['title']) && $oFilter->aData['title']) $oUrl->setParam('title', $oFilter->aData['title']);
		$oUrl->setParam('sortby', $aForm['sortby']);

	    $oTpl->assignSrc('aFilter', $oFilter->aHtml);
        $oTpl->assignSrc(array(
			'iFrom' => ($iCnt) ? ($iFrom + 1) : 0,
	        'iTo' => $iTo,
            'aItems'    => $aItems,
            'sCategory' => $sCategory,
        	'aCategories'=>$aCategories,
	        'bFiltered' => ($oReq->getAction() == 'browse_group_show') ? true : false,
            'iCnt'      => $iCnt,
            'sImageUrl' 	=> App::getParam('groups.pic.url'),
            'aPaging'   => $oPager->getInfo($oUrl),
        	'aForm'		=> $aForm,
        	'sMonthDateFormat' => App::getParam('format.monthdate%'),
        	'bZipState'    => App::getSetting('us_states')
        ));
        $srcHtml = $oTpl->fetch('Browse.html');
            return $srcHtml;
    }
}
?>